 //控制层 
app.controller('sellerController' ,function($scope,$controller ,sellerService){
	
	$controller('baseController',{$scope:$scope});//继承

	/**
	 * 用户注册
	 */
	$scope.reg = function(){
		sellerService.add($scope.entity).success(function (response) {
			if(response.success){
				location.href="../../shoplogin.html";
			}else{
				alert(response.message);
			}
		})
	}
	/**
	 * 它这个sellerId需要我们去后台获取,前台初始化就可以了
	 */
	$scope.findOne=function(){
		sellerService.findOne().success(
			function(response){
				$scope.entity= response;
			}
		);
	}
	//验证唯一用户名
	$scope.findSellerById = function(sellerId){
		sellerService.findSellerById(sellerId).success(function (response) {
			if(response.success){
				$scope.msgcolor= "#32cd32";
				$scope.message = response.message;
			}else{
				$scope.msgcolor= "red";
				$scope.message = response.message;
			}

		})
	}
	/**
	 * 修改用户信息
	 */
	$scope.update=function(){
		serviceObject=sellerService.update( $scope.entity ).success(
			function (response) {
				if(response.success){
					alert("保存成功")
					$scope.findOne();
				}else{
					alert(response.message);
				}
			})
	}

	/**
	 * 修改密码
	 */
	$scope.upaPassword=function(){
		if($scope.newPassword!=$scope.newPassword2){
			alert("两次输入的新密码不一致")
		}else{
			if($scope.newPassword!=$scope.entity.password){
				sellerService.upaPassword($scope.entity,$scope.newPassword).success(
					function (response) {
						if(response.success){
							alert(response.message)
						}else{
							alert(response.message);
						}
					})
			}else{
				alert("新密码与旧密码不能一致")
			}
		}
	}
    //读取列表数据绑定到表单中  
	$scope.findAll=function(){
		sellerService.findAll().success(
			function(response){
				$scope.list=response;
			}			
		);
	}    
	
	//分页
	$scope.findPage=function(page,rows){			
		sellerService.findPage(page,rows).success(
			function(response){
				$scope.list=response.rows;	
				$scope.paginationConf.totalItems=response.total;//更新总记录数
			}			
		);
	}
	

	//保存，要是把成功之后的操作改了，修改有出问题
	$scope.save=function(){				
		var serviceObject;//服务层对象  				
		if($scope.entity.id!=null){//如果有ID
			serviceObject=sellerService.update( $scope.entity ); //修改  
		}else{
			serviceObject=sellerService.add( $scope.entity);//增加
		}				
		serviceObject.success(
			function(response){
				if(response.success){
					//重新查询.这个适合在页面里面，不适合注册
		        	$scope.reloadList();//重新加载
				}else{
					alert(response.message);
				}
			}		
		);				
	}
	
	 
	//批量删除 
	$scope.dele=function(){			
		//获取选中的复选框			
		sellerService.dele( $scope.selectIds ).success(
			function(response){
				if(response.success){
					$scope.reloadList();//刷新列表
				}						
			}		
		);				
	}
	
	$scope.searchEntity={};//定义搜索对象 
	
	//搜索
	$scope.search=function(page,rows){			
		sellerService.search(page,rows,$scope.searchEntity).success(
			function(response){
				$scope.list=response.rows;	
				$scope.paginationConf.totalItems=response.total;//更新总记录数
			}			
		);
	}
    
});	
